//
//  UIButton+Extension.swift
//  AIPhotoProcess
//
//  Created by aw031 on 17/10/2025.
//

import UIKit

// MARK: - ButtonEdgeInsetsStyle 枚举定义
/// 按钮图片与标题布局样式
enum ButtonEdgeInsetsStyle {
    case top
    case left
    case bottom
    case right //文字 向右
}

// 使用关联对象存储 Block

//private let eventTouchBlockKey = "EventTouchBlockKey"
private var eventTouchBlockKey: UInt8 = 0

private let eventLeftKey = "LeftKey"
private let eventRightkKey = "RightkKey"
private let eventTopKey = "TopKey "
private let eventBottomKey = "BottomKey"


// MARK: - UIButton 扩展
extension UIButton {

    /// 点击事件回调 Block
    var eventTouchBlock: EventClickBlock? {
//        get {
//            return objc_getAssociatedObject(self, &eventTouchBlockKey) as? EventClickBlock
//        }
//        set {
//            objc_setAssociatedObject(self, &eventTouchBlockKey, newValue, .OBJC_ASSOCIATION_COPY_NONATOMIC)
//        }
        get {
             return objc_getAssociatedObject(self, &eventTouchBlockKey) as? EventClickBlock
         }
        set {
            objc_setAssociatedObject(self, &eventTouchBlockKey, newValue, .OBJC_ASSOCIATION_COPY_NONATOMIC)
        }
    }
    

    /// 添加点击事件回调 Block
    func addEventTouchBlock(_ eventBlock: @escaping EventClickBlock) {
        self.eventTouchBlock = eventBlock
        self.addTarget(self, action: #selector(handleTouch), for: .touchUpInside)
    }

    @objc private func handleTouch() {

        eventTouchBlock?(self)
    }
    
    

    
    
    /// 设置按钮背景图片（默认状态）
  @objc func setCustomBackgroundImage(_ image: UIImage?) {
        self.setBackgroundImage(image, for: .normal)
        self.setBackgroundImage(image, for: .disabled)
        self.setBackgroundImage(image, for: .highlighted)
    }

    /// 设置按钮背景图片（指定状态）
    @objc func setCustomBackgroundImage(_ image: UIImage?, for state: UIControl.State) {
        self.setBackgroundImage(image, for: state)
    }
    
    /// 设置按钮背景图片（默认状态）
    func setCustomImage(_ image: UIImage?) {
        self.setImage(image, for: .normal)
        self.setImage(image, for: .disabled)
        self.setImage(image, for: .highlighted)
    }
    
    /// 设置按钮背景图片（指定状态）
    func setCustomImage(_ image: UIImage?, for state: UIControl.State) {
        self.setImage(image, for: state)
    }
    


}

extension UIButton {
        
    
    /// 设置按钮点击区域的扩展边距
    func setEventTouchEdge(top: CGFloat, right: CGFloat, bottom: CGFloat, left: CGFloat) {
        // 具体实现需参考 Objective-C 版本
        objc_setAssociatedObject(self, eventTopKey,NSNumber(value: top) , .OBJC_ASSOCIATION_COPY_NONATOMIC);
        objc_setAssociatedObject(self, eventRightkKey, NSNumber(value: right), .OBJC_ASSOCIATION_COPY_NONATOMIC);
        objc_setAssociatedObject(self, eventBottomKey, NSNumber(value: bottom), .OBJC_ASSOCIATION_COPY_NONATOMIC);
        objc_setAssociatedObject(self, eventLeftKey, NSNumber(value: left), .OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
    
    
    func setEnlargeEdge(top: CGFloat, right: CGFloat, bottom: CGFloat, left: CGFloat) {
         objc_setAssociatedObject(self, eventTopKey, NSNumber(value: top), .OBJC_ASSOCIATION_COPY_NONATOMIC)
         objc_setAssociatedObject(self, eventRightkKey, NSNumber(value: right), .OBJC_ASSOCIATION_COPY_NONATOMIC)
         objc_setAssociatedObject(self, eventBottomKey, NSNumber(value: bottom), .OBJC_ASSOCIATION_COPY_NONATOMIC)
         objc_setAssociatedObject(self, eventLeftKey, NSNumber(value: left), .OBJC_ASSOCIATION_COPY_NONATOMIC)
     }
     

    open override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
         let rect = enlargedRect()
         if rect.equalTo(bounds) {
             return super.point(inside: point, with: event)
         }
         return rect.contains(point)
     }
    
    func enlargedRect() -> CGRect {
        let topEdge = objc_getAssociatedObject(self, eventLeftKey) as? NSNumber
        let rightEdge = objc_getAssociatedObject(self, eventLeftKey) as? NSNumber
        let bottomEdge = objc_getAssociatedObject(self,eventLeftKey) as? NSNumber
        let leftEdge = objc_getAssociatedObject(self, eventLeftKey) as? NSNumber
        
        if let top = topEdge?.floatValue,
           let right = rightEdge?.floatValue,
           let bottom = bottomEdge?.floatValue,
           let left = leftEdge?.floatValue {
            return CGRect(x: bounds.origin.x - CGFloat(left),
                          y: bounds.origin.y - CGFloat(top),
                          width: bounds.size.width + CGFloat(left) + CGFloat(right),
                          height: bounds.size.height + CGFloat(top) + CGFloat(bottom))
        } else {
            return bounds
        }
    }
    
    
    
    // MARK: - Layout Button
    
    func layoutButtonTitleImageEdgeInsets(style: ButtonEdgeInsetsStyle, titleImgSpace space: CGFloat) {
        // Get label size
        var labelW: CGFloat = 0
        var labelH: CGFloat = 0
        
        // Get image size
        let imageW = imageView?.frame.size.width ?? 0
        let imageH = imageView?.frame.size.height ?? 0
        
        if #available(iOS 8.0, *) {
            let labelSize = titleLabel?.intrinsicContentSize ?? .zero
            labelW = min(labelSize.width, frame.size.width - imageW)
            labelH = min(labelSize.height, frame.size.height)
        } else {
            labelW = titleLabel?.frame.size.width ?? 0
            labelH = titleLabel?.frame.size.height ?? 0
        }
        
        var imageEdgeInsets = UIEdgeInsets.zero
        var titleEdgeInsets = UIEdgeInsets.zero
        
        switch style {
        case .left:
            // Text on left, image on right
            imageEdgeInsets = UIEdgeInsets(top: 0, left: labelW + space/2, bottom: 0, right: -labelW - space/2)
            titleEdgeInsets = UIEdgeInsets(top: 0, left: -imageW - space/2, bottom: 0, right: imageW + space/2)
            
        case .right:
            // Text on right, image on left
            imageEdgeInsets = UIEdgeInsets(top: 0, left: -space/2, bottom: 0, right: space/2)
            titleEdgeInsets = UIEdgeInsets(top: 0, left: space/2, bottom: 0, right: -space/2)
            
        case .top:
            // Text on top, image on bottom
            imageEdgeInsets = UIEdgeInsets(top: 0, left: -labelW/2, bottom: -labelH - space/2, right: -labelW)
            titleEdgeInsets = UIEdgeInsets(top: 0, left: -imageW/2, bottom: imageH + space/2, right: 0)
            
        case .bottom:
            // Text on bottom, image on top
            imageEdgeInsets = UIEdgeInsets(top: -labelH/2 - space/4, left: labelW/2, bottom: labelH/2 + space/4, right: -labelW/2)
            titleEdgeInsets = UIEdgeInsets(top: imageH/2 + space/4, left: -imageW/2, bottom: -imageH/2 - space/4, right: imageW/2)
        }
        
        self.titleEdgeInsets = titleEdgeInsets
        self.imageEdgeInsets = imageEdgeInsets
    }
    
    
}
